package reduce.fz.com.reduce.activity;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.HashMap;
import java.util.List;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.TargetSet;
import reduce.fz.com.reduce.bean.TeamDynamic;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.LoadingDialog;
import reduce.fz.com.reduce.event.PubDynamicEvent;
import reduce.fz.com.reduce.event.TargetSetEvent;
import reduce.fz.com.reduce.fragment.TargetFollowFragment;
import reduce.fz.com.reduce.service.TeamService;
import reduce.fz.com.reduce.service.preference.UserPreference;
import reduce.fz.com.reduce.util.ServiceBuilder;
import reduce.fz.com.reduce.util.TeamDynamicUtil;
import reduce.fz.com.reduce.util.base.ReduceApplication;

public class TargetFollowActivity extends BaseToolBarActivity  implements View.OnClickListener{

    private FrameLayout mContainer;
    private TargetFollowFragment fragment;
    private TargetSet targetSet;
    private TeamService teamService= ServiceBuilder.builderTeamService(null);
    private LoadingDialog loadingDialog ;
    private TextView rightBtn;

    public void attachFragment()
    {
        fragment=TargetFollowFragment.create(targetSet);
        getSupportFragmentManager().beginTransaction().replace(R.id.targetset_recommend_container,fragment).commit();

    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_target_follow);
        setToolbarTitle("关注您可能感兴趣的人");
        rightBtn=setRightBtn("完成");
        rightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hasfinish();
            }
        });
        targetSet= (TargetSet) getIntent().getSerializableExtra("targetset");
        initWidget();
        loadingDialog=new LoadingDialog(this);
        attachFragment();
    }

    @Override
    public void initWidget() {
        mContainer=(FrameLayout)findViewById(R.id.targetset_recommend_container);
       // mFinish=(TextView)findViewById(R.id.tv_finish);
       // mFinish.setOnClickListener(this);

    }

    @Override
    public void initListener() {
    }
    public void hasfinish()
    {
       List<Long> ids= fragment.getFollowUids();
        List<User> users=fragment.getFollowUsers();
        targetSet.focusUids=ids;
        targetSet.focusUsers=users;
        teamService.targetSetting(targetSet, new AbResponseListener<HashMap>(new TypeReference<Result<HashMap>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                loadingDialog.dismiss();
            }
            public void responseInfo(Result<HashMap> data) {
                loadingDialog.dismiss();
                User user= UserPreference.getUser();
                TeamDynamic dynamic = TeamDynamicUtil.targetSet(user, targetSet);
                PubDynamicEvent event = new PubDynamicEvent(dynamic);
                EventBus.getDefault().post(event);
                EventBus.getDefault().post(new TargetSetEvent(targetSet));
                ReduceApplication.getInstance().finishActivity(TargetSettingActivity.class);
                finish();

            }
        });


    }


    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            default:
                break;
        }


    }
}
